<?php

class Core_Backend_Component_Design_Controller_Page_Position extends Dcmp_Component_Controller {
	
    public function indexAction($integer_pageindex = 1, $integer_pagesize = 20, $sort = 'id', $dir = 'desc') {
        $this->getHistory()->record();
        $validator = $this->getRequest()->getValidator('filter');
        $collection = App::getModel('design/collection_page_position')
                        ->joinTemplate()
                        ->filterByAreaId($this->getSession()->get('managed_area_id'))
                        ->pagination($integer_pageindex, $integer_pagesize);
        if($validator->isNotEmpty('code')){
            $collection->addParameter('code', '%'.$validator->getValue('code').'%')
                       ->like('a.code', ':code:s');
        }
        if($validator->isNotEmpty('name')){
            $collection->addParameter('a.name', ':name:s')
                       ->like('name', '%'.$validator->getValue('name').'%');
        }
        if($validator->isNotEmpty('template')){
            $collection->filterByTemplateId($validator->getValue('template'));
        }
        if($validator->isNotEmpty('enabled')){
            $collection->filterByEnabled($validator->getValue('enabled'));
        }
        $collection->sortBy($sort, $dir);
        $collection->load();
        $records = $collection->loadCount();
        $templates = App::getModel('design/collection_page_template')
                        ->filterByAreaId($this->getSession()->get('managed_area_id'))
                        ->load();
        return $this->viewResult(array('list' => $collection, 'records' => $records, 'templates'=>$templates));
    }

    public function createAction() {
        $pageThemes = App::getModel('design/collection_page_theme')
                        ->filterByEnabled(1)
                        ->load();
        $pageTemplates = App::getModel('design/collection_page_template')
                        ->filterByAreaId($this->getSession()->get('managed_area_id'))
                        ->filterByEnabled(1)
                        ->load();
        return $this->viewResult(array('page_templates'=>$pageTemplates, 'page_themes'=>$pageThemes));
    }

    public function editAction($id) {
        $model = App::getModel('design/page_position')->load($id);
        return $this->createAction()->setModel($model);
    }

    public function applyPostAction() {
        return $this->saveModel(FALSE);
    }

    public function savePostAction() {
        return $this->saveModel();
    }

    private function saveModel($return = TRUE) {
        $data = $this->getRequest()->getForm('position');
        $model = App::getModel('design/page_position');
        if (isset($data['id']) && !empty($data['id'])) {
            $model->load($data['id']);
        }
        $model->addData($data);
        if (($errors = $model->validate()) === TRUE) {
            $model->save();
            $this->getMessager()->addInform($this->__('share.message.success'));
            return $return ?
                    $this->redirectResult($this->getHistoryUrl('design/page_position/index')):
                    $this->redirectResult($this->getUrl(array('id' => $model->getIdValue()), 'edit'));

        } else {
            $this->getMessager()
                 ->addError($this->__('share.message.fail'))
                 ->addError($errors);
            return $this->viewResult(array('model' => $model));
        }
    }

}